/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package testapp;

import comunicacao.ReceiveMessageInterface;
import static java.awt.SystemColor.text;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.ArrayList;
import entities.Teatro;

/**
 *
 * @author diogo
 */
public class AppDatabaseComunicator {

    String serverAddress;
    String serverPort;
    Registry registry;
    ReceiveMessageInterface dbServer;

    public AppDatabaseComunicator(String serverAddress, String serverPort) {

        this.serverAddress = serverAddress;
        this.serverPort = serverPort;

        System.out.println("sending " + text + " to " + serverAddress + ":" + serverPort);

        try {
            registry = LocateRegistry.getRegistry(
                    serverAddress,
                    (new Integer(serverPort)).intValue()
            );
            dbServer = (ReceiveMessageInterface) (registry.lookup("dbServer"));
        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (NotBoundException e) {
            e.printStackTrace();
        }
    }
    
    
    
	public ArrayList<String> listTheaters(){
            try{
            return dbServer.listTheaters();
            }catch(RemoteException e){
                return null;
            }
        }

	public Teatro getTheater(String theaterName){
            try {
                return dbServer.getTheater(theaterName);
            } catch (RemoteException e) {
                return null;
            }
        }

	public boolean reserveSeat(String theaterName, int lugarID, int userID){
            try {
                return dbServer.reserveSeat(theaterName, lugarID, userID);
            } catch (RemoteException e) {
                return false;
            }
        }

	public boolean purchaseSeat(String theaterName, int lugarID, int userID){
            try {
                return dbServer.purchaseSeat(theaterName, lugarID, userID);
            } catch (RemoteException e) {
                return false;
            }
        }
        
        public boolean falha(){
            try {
                return dbServer.falha();
            } catch (RemoteException e) {
                return false;
            }
        }
        
        public boolean restart(){
            boolean result = false;
            
            try {
                dbServer = (ReceiveMessageInterface) (registry.lookup("dbServertemp"));			
                result = dbServer.restart();
		dbServer = (ReceiveMessageInterface) (registry.lookup("dbServer"));
                return result;
            } catch (RemoteException e) {
                return false;
            } catch(NotBoundException e){
                return false;
            }
        }
}
